package com.peng.rpc.entity;

import com.peng.rpc.enumeration.ResponseCode;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @author Wonderson
 * @email wpp.ds@foxmail.com
 * @date 2022/2/27 18:55
 */
@Data
@NoArgsConstructor
public class RpcResponse<T> implements Serializable {

    //对应请求包序列号
    private String requestId;

    private int responseCode;

    private String message;

    private T data;

    /**
     * 成功远程响应数据
     */
    public static <T> RpcResponse<T> success(T data, String requestId) {
        RpcResponse<T> success = new RpcResponse<>();
        success.setRequestId(requestId);
        success.setResponseCode(ResponseCode.SUCCESS.getCode());
        success.setMessage(ResponseCode.SUCCESS.getMessage());
        success.setData(data);
        return success;
    }

    /**
     * 失败远程会按照失败的类型响应状态码
     */
    public static <T> RpcResponse<T> fail(ResponseCode code, String requestId) {
        RpcResponse<T> fail = new RpcResponse<>();
        fail.setRequestId(requestId);
        fail.setResponseCode(code.getCode());
        fail.setMessage(code.getMessage());
        return fail;
    }

}
